package org.hrqing.authorization.mapstruct;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hrqing.authorization.entity.User;
import org.hrqing.authorization.security.SecurityUser;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;

/**
 * @auther Hrqing
 */
@Mapper(builder = @Builder(disableBuilder = true))
public interface UserConvertMapper {

    UserConvertMapper INSTANCE = Mappers.getMapper(UserConvertMapper.class);

    @Mappings({
            @Mapping(target = "authorities", ignore = true),
    })
    SecurityUser convertUserToSecurityUser(User user);

    default SecurityUser convertUserToSecurityUser(User user, List<SimpleGrantedAuthority> authorities) {
        SecurityUser securityUser = convertUserToSecurityUser(user);
        securityUser.setAuthorities(authorities);
        return securityUser;
    }
}
